package com.platform.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.platform.dto.ConfTypeQo;
import com.platform.dto.ConfTypeTreeNode;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 非递归方式生成树结构
 *
 * @author qy
 * @date Created in 2020/7/16
 **/
public class TreeNotRecursion {

    /**
     * 转换类别信息列表为类别信息树结构
     *
     * @param origin 原始数据
     * @return List<TreeNode>
     */
    public static List<ConfTypeTreeNode> transListToTreeNode(List<ConfTypeQo> origin) {
        //转为ConfTypeTreeNode并分组
        Map<String, List<ConfTypeTreeNode>> collect = origin.stream()
                .map(confTypeQo -> ConfTypeTreeNode.of(confTypeQo.getId(),
                        confTypeQo.getTypeCode(),
                        confTypeQo.getTypeName(),
                        Objects.toString(confTypeQo.getParentId(),""),
                        confTypeQo.getLevel(),
                        confTypeQo.getDesc(),
                        confTypeQo.getSortNum().toString(),
                        confTypeQo.getRoute(),
                        null))
                .sorted(Comparator.comparing(ConfTypeTreeNode::getSortNum))
                .collect(Collectors.groupingBy(ConfTypeTreeNode::getParentId));


        Map<String, List<ConfTypeTreeNode>> maps = Maps.newHashMap();
        collect.forEach((key, children) -> children.forEach(node -> {
            if (collect.containsKey(node.getId())) {
                node.setChildren(collect.get(node.getId()));
                maps.put(node.getId(), collect.get(node.getId()));
            }
        }));

        //过滤重复的子级数据
        List<ConfTypeTreeNode> result = Lists.newArrayList();
        collect.entrySet().stream().filter(e -> !maps.containsKey(e.getKey())).forEach(e -> result.addAll(e.getValue()));

        return result;
    }

    /**
     * 转换类别信息列表为类别信息树结构
     *
     * @param origin 原始数据
     * @return List<TreeNode>
     */
    public static List<ConfTypeTreeNode> transListToTreeNodeWithRouteCode(List<ConfTypeQo> origin) {
        //转为ConfTypeTreeNode并分组
        Map<String, List<ConfTypeTreeNode>> collect = origin.stream()
                .map(confTypeQo -> ConfTypeTreeNode.of(confTypeQo.getId(),
                        confTypeQo.getRoute(),
                        confTypeQo.getTypeName(),
                        Objects.toString(confTypeQo.getParentId(),"")))
                .collect(Collectors.groupingBy(ConfTypeTreeNode::getParentId));

        Map<String, List<ConfTypeTreeNode>> maps = Maps.newHashMap();
        collect.forEach((key, children) -> children.forEach(node -> {
            if (collect.containsKey(node.getId())) {
                node.setChildren(collect.get(node.getId()));
                maps.put(node.getId(), collect.get(node.getId()));
            }
        }));

        //过滤重复的子级数据
        List<ConfTypeTreeNode> result = Lists.newArrayList();
        collect.entrySet().stream().filter(e -> !maps.containsKey(e.getKey())).forEach(e -> result.addAll(e.getValue()));

        return result;
    }
}
